<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_perakitan_produk extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_perakitan_produk_model', 'IspPerakitanProduk');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Perakitan Produk";		

		$cek_data = $this->db->query("SELECT * FROM (SELECT (CAST(id_perakitan_barang AS INTEGER) + 1) as num FROM ISP_PERAKITAN_BARANG order by id_perakitan_barang desc) x WHERE ROWNUM <= 1");	
		if ($cek_data->num_rows() > 0) {
			$id_x = $cek_data->row_array();
			$id = $id_x['num'];
		} else {
			$id = 1;
		}

		$data['id'] = $id;
		$data['mode'] = 'create';

		viewByTemplate('isp_perakitan_produk/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspPerakitanProduk->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspPerakitanProduk->GetJSON2();
	}


	function getdata3()
	{
		$data['id']= $this->input->post('id_perakitan_barang');
		$this->secure_session();
		echo $this->IspPerakitanProduk->GetJSON3(get('id_perakitan_barang'));
	}

	function getdata_detail()
	{

		$data['id']= $this->input->post('id_perakitan_barang');
		$this->load->model('isp_perakitan_produk_model',$data);
		$this->secure_session();
		echo $this->IspPerakitanProduk->GetJSON_detail(post('id_perakitan_barang'));
	}

	function dtl_sample2_data()
	{
		// Ajax Response For Datagrid
		echo $this->IspPerakitanProduk->GetJSON_detail(post('id_perakitan_barang'));
	}

	function create()
	{
		// Display Form Create
		$data['title'] = "New Perakitan Produk";
		$data['data'] = NULL;

		$cek_data = $this->db->query("SELECT * FROM (SELECT (CAST(id_perakitan_barang AS INTEGER) + 1) as num FROM ISP_PERAKITAN_BARANG order by id_perakitan_barang desc) x WHERE ROWNUM <= 1");	
		if ($cek_data->num_rows() > 0) {
			$id_x = $cek_data->row_array();
			$id = $id_x['num'];
		} else {
			$id = 1;
		}

		$data['id'] = $id;

		$query3 = $this->db->query("SELECT id_perakitan_barang_detail FROM (SELECT * FROM isp_perakitan_barang_detail ORDER BY id_perakitan_barang_detail DESC) WHERE rownum <=1");
	 					if ($query3->row_array()) {
	 						$getquery3 = $query3->row_array();
	 						$rowcount3 = $getquery3['id_perakitan_barang_detail'];
	 						$autonum3 = $rowcount3 + 1;
	 					} else {
					$autonum3 = 1;
				}

		$data['id_perakitan_detail'] = $autonum3; 
		
		$data['mode'] = "create";
		viewByTemplate('isp_perakitan_produk/form', $data, 'form');
	}

	function IdDetail(){
		$query3 = $this->db->query("SELECT id_perakitan_barang_detail FROM (SELECT * FROM isp_perakitan_barang_detail ORDER BY id_perakitan_barang_detail DESC) WHERE rownum <=1");
	 					if ($query3->row_array()) {
	 						$getquery3 = $query3->row_array();
	 						$rowcount3 = $getquery3['id_perakitan_barang_detail'];
	 						$autonum3 = $rowcount3 + 1;
	 					} else {
					$autonum3 = 1;
				}

		$data['id_perakitan_detail'] = $autonum3;

		echo json_encode($data);
	}

	function edit()
	{
		// Display Form Edit
		$data['title'] = "Edit Perakitan Produk";
		$data['data'] = $this->IspPerakitanProduk->GetSingleView(get('id_perakitan_barang'));
		
		$cek_data = $this->db->query("SELECT * FROM (SELECT (CAST(id_perakitan_barang AS INTEGER) + 1) as num FROM ISP_PERAKITAN_BARANG order by id_perakitan_barang desc) x WHERE ROWNUM <= 1");	
		if ($cek_data->num_rows() > 0) {
			$id_x = $cek_data->row_array();
			$id = $id_x['num'];
		} else {
			$id = 1;
		}

		$data['id'] = $id;

		$query3 = $this->db->query("SELECT id_perakitan_barang_detail FROM (SELECT * FROM isp_perakitan_barang_detail ORDER BY id_perakitan_barang_detail DESC) WHERE rownum <=1");
	 					if ($query3->row_array()) {
	 						$getquery3 = $query3->row_array();
	 						$rowcount3 = $getquery3['id_perakitan_barang_detail'];
	 						$autonum3 = $rowcount3 + 1;
	 					} else {
					$autonum3 = 1;
				}

		$data['id_perakitan_detail'] = $autonum3; 
		// Define Combo Data
		$data['mode'] = "edit";
		viewByTemplate('isp_perakitan_produk/form', $data, 'form');
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		 if (post("id_perakitan_barang") == '') {
		 	$error->Add("id_perakitan_barang", "Kode Produk", $tpl_empty);
		 } else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_perakitan_barang WHERE id_perakitan_barang = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("id_perakitan_barang"))))."'")) > 0) {
		 	$error->Add("id_perakitan_barang", "ID Perakitan", $tpl_duplicate);
		 }
		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				//Set Auto Increment
				$query2 = $this->db->query("SELECT id_perakitan_barang FROM (SELECT * FROM isp_perakitan_barang ORDER BY id_perakitan_barang DESC) WHERE rownum <=1");
	 					if ($query2->row_array()) {
	 						$getquery2 = $query2->row_array();
	 						$rowcount2 = $getquery2['id_perakitan_barang'];
	 						$autonum2 = $rowcount2 + 1;
	 					} else {
					$autonum2 = 1;
				}

				// Fill Post Data
				$data	= array('id_perakitan_barang' =>$autonum2,
								'id_produk' =>$this->input->post('id_produk'),
								'kode_jenis_barang' =>$this->input->post('kode_jenis_barang'),
								'nama_jenis_barang' =>$this->input->post('nama_jenis_barang'),
								'bandwidth' =>$this->input->post('bandwidth')
						);

				if (post("mode") == "create")
				{
					$this->IspPerakitanProduk->Add($data);
					$ls_response = "Data Tersimpan";
				}
				else if (post("mode") == "edit")
				{
					$this->IspPerakitanProduk->Update(	$data, array('id_perakitan_barang'	=> $data['id_perakitan_barang']));
					$ls_response = "Data Tersimpan [edited]";
				}
								
				// ----- >> Detail Sample 2
				$dtl_sample2	= $this->data['dtl_sample2'];
				$total_deleted	= $dtl_sample2['num_row']['deleted'];
				$total_inserted	= $dtl_sample2['num_row']['inserted'];
				$total_updated	= $dtl_sample2['num_row']['updated'];
				
				for($i=0,$max_i=$total_deleted; $i<$max_i; $i++){
					$row = $dtl_sample2['deleted'][$i];					
					$this->db->delete('ISP_PERAKITAN_BARANG_DETAIL', array('id_perakitan_barang_detail'=> $row['id_perakitan_barang_detail']));
				}
				
				for($i=0,$max_i=$total_inserted; $i<$max_i; $i++){
					$query = $this->db->query("SELECT id_perakitan_barang_detail FROM (SELECT * FROM isp_perakitan_barang_detail ORDER BY id_perakitan_barang_detail DESC) WHERE rownum <=1");
	 					if ($query->row_array()) {
	 						$getquery = $query->row_array();
	 						$rowcount = $getquery['id_perakitan_barang_detail'];
	 						$autonum = $rowcount + 1;
	 					} else {
					$autonum = 1;
				}

					$row = $dtl_sample2['inserted'][$i];
					$row_data['kode_barang'] = $row['kode_produk'];
					$row_data['id_inventori'] = $row['id_inventori'];
					$row_data['banyak'] = (float)$row['banyak'];
					$row_data['id_perakitan_barang_detail'] = $autonum;
					
					if (post("mode") == "create"){
						$row_data['id_perakitan_barang'] = $autonum2;
					}					
					else if (post("mode") == "edit"){
						$row_data['id_perakitan_barang'] = $this->input->post('id_perakitan_barang');
					}

					$this->db->insert('ISP_PERAKITAN_BARANG_DETAIL',$row_data);
				}
					
				for($i=0,$max_i=$total_updated; $i<$max_i; $i++){
					$row = $dtl_sample2['updated'][$i];		
					$row_data['kode_barang'] = $row['kode_produk'];
					$row_data['id_inventori'] = $row['id_inventori'];
					$row_data['banyak'] = (float)$row['banyak'];							
					$this->db->update('I\SP_PERAKITAN_BARANG_DETAIL',$row_data, array('id_perakitan_barang_detail'=> $row['id_perakitan_barang_detail']));
				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result		= "";
		$pesan			= "";
		$kode_produk 	= get('id_perakitan_barang');
		
		if ($kode_produk != "")
		{
			// Delete Single
			$arr_rule	= array('id_perakitan_barang' => $kode_produk);
			$ls_result = $this->IspPerakitanProduk->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_perakitan_barang' => $row['id_perakitan_barang']);
				$ls_result .= $this->IspPerakitanProduk->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>loadPage('/'+controller+'/index');</script>
		";
	}
}
?>